package com.agricultural.machine.path;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 地形优化路径策略 - 策略模式
 */
public class TerrainOptimizedPathStrategy implements PathStrategy {
    private final Random random = new Random();
    
    @Override
    public String getName() {
        return "基于农田状况优化路径";
    }

    @Override
    public String getDescription() {
        return "基于农田状况优化径策略 - 根据地形条件（坡度、湿度、土壤类型）优化路径";
    }

    @Override
    public List<PathPoint> calculatePath(PathPoint start, PathPoint end, TerrainData terrain) {
        List<PathPoint> path = new ArrayList<>();
        path.add(start);
        
        System.out.println("地形分析: 宽度=" + terrain.getWidth() + ", 高度=" + terrain.getHeight() + 
                           ", 坡度=" + terrain.getSlope() + ", 湿度=" + terrain.getMoisture() + 
                           ", 土壤类型=" + terrain.getSoilType());
        
        // 计算路径点
        double lastX = start.x;
        double lastY = start.y;
        
        // 根据坡度调整路径
        if (terrain.getSlope() > 10) {
            System.out.println("坡度较大，使用缓坡路径");
            // 使用之字形路径减缓坡度
            double distX = end.x - start.x;
            double distY = end.y - start.y;
            int zigzags = 3 + random.nextInt(3);
            
            for (int i = 1; i <= zigzags; i++) {
                double ratio = i / (double)(zigzags + 1);
                double x = start.x + distX * ratio;
                double y = start.y + distY * ratio;
                
                // 添加之字形偏移
                double offset = 10 * (i % 2 == 0 ? 1 : -1);
                x += offset;
                
                path.add(new PathPoint(x, y));
            }
        } 
        // 根据湿度调整路径
        else if (terrain.getMoisture() > 60) {
            System.out.println("地面湿度较高，避开低洼区域");
            // 避开湿地，使用弧形路径
            double midX = (start.x + end.x) / 2;
            double midY = (start.y + end.y) / 2;
            
            // 弧形偏移
            double offsetY = Math.max(20, Math.abs(end.y - start.y) * 0.2);
            
            // 添加弧形路径点
            path.add(new PathPoint(midX, midY + offsetY));
        } 
        // 根据土壤类型调整路径
        else if (terrain.getSoilType().equals("松软")) {
            System.out.println("土壤松软，寻找硬地通过");
            // 寻找硬地通过，添加多个中间点
            int pointCount = 4 + random.nextInt(3);
            
            for (int i = 1; i < pointCount; i++) {
                double ratio = i / (double)pointCount;
                double x = start.x + (end.x - start.x) * ratio;
                double y = start.y + (end.y - start.y) * ratio;
                
                // 随机偏移模拟寻找硬地
                x += (random.nextDouble() - 0.5) * 10;
                y += (random.nextDouble() - 0.5) * 10;
                
                path.add(new PathPoint(x, y));
            }
        } 
        // 默认使用直线路径
        else {
            System.out.println("地形条件良好，使用直线路径");
            double distance = Math.sqrt(Math.pow(end.x - lastX, 2) + Math.pow(end.y - lastY, 2));
            int pointCount = (int)(distance / 10) + 1;
            
            for (int i = 1; i < pointCount; i++) {
                double ratio = i / (double)pointCount;
                double x = lastX + (end.x - lastX) * ratio;
                double y = lastY + (end.y - lastY) * ratio;
                path.add(new PathPoint(x, y));
            }
        }
        
        path.add(end);
        
        System.out.println("使用基于农田状况优化路径策略计算从 " + start + " 到 " + end + " 的路径");
        System.out.println("生成路径点数量: " + path.size());
        
        return path;
    }
} 